package com.gas.project.gas.student.service.impl;

import java.util.List;

import com.gas.common.utils.DateUtils;
import com.gas.common.utils.StringUtils;
import com.gas.common.utils.text.Convert;
import com.gas.project.gas.student.domain.GraduateStudent;
import com.gas.project.gas.student.domain.StudentAuditDetail;
import com.gas.project.system.user.domain.User;
import com.gas.project.system.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import com.gas.project.gas.student.mapper.StudentMapper;
import com.gas.project.gas.student.domain.Student;
import com.gas.project.gas.student.service.IStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生信息管理Service业务层处理
 *
 * @author xyl
 * @date 2022-04-01
 */
@Service
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 查询学生信息管理
     *
     * @param studentNumber 学生信息管理ID
     * @return 学生信息管理
     */
    @Override
    public Student selectStudentById(String studentNumber) {
        return studentMapper.selectStudentById(studentNumber);
    }

    /**
     * 查询学生信息管理列表
     *
     * @param student 学生信息管理
     * @return 学生信息管理
     */
    @Override
    public List<Student> selectStudentList(Student student) {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生信息管理
     *
     * @param student 学生信息管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStudent(Student student) {
        student.setCreateTime(DateUtils.getNowDate());
        userService.insertUser(loadAddStudentToUser(student));
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改学生信息管理
     *
     * @param student 学生信息管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStudent(Student student) {
        student.setUpdateTime(DateUtils.getNowDate());
        int i = studentMapper.updateStudent(student);

        User temp = userService.selectUserByLoginName(student.getStudentNumber());
        User user = loadUpdateStudentToUser(student);
        user.setUserId(temp.getUserId());
        int i1 = userService.updateUserInfo(user);
        if(StringUtils.isNotEmpty(student.getGraduationStatus())){
            SimpleMailMessage message=new SimpleMailMessage();
            message.setFrom("1973812565@qq.com");
            message.setTo("809063051@qq.com");
            message.setSubject("毕设审核提醒");
            message.setText("您的毕业审核状态为："+ student.getGraduationStatus());
            javaMailSender.send(message);
        }
        return i + i1;
    }

    /**
     * 删除学生信息管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteStudentByIds(String ids) {
        int i = studentMapper.deleteStudentByIds(Convert.toStrArray(ids));
        int i1 = userService.deleteUserByLoginNames(Convert.toStrArray(ids));
        return i + i1;
    }

    /**
     * 删除学生信息管理信息
     *
     * @param studentNumber 学生信息管理ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStudentById(String studentNumber) {
        int i1 = userService.deleteUserByLoginName(studentNumber);
        return studentMapper.deleteStudentById(studentNumber);
    }

    @Transactional
    @Override
    public String importStudent(List<Student> studentList) {
        //导入学生信息逻辑
        //需要将学号等数据进行对应
        for (Student student : studentList) {
            userService.insertUser(loadAddStudentToUser(student));
            studentMapper.insertStudent(student);
        }
        return "导入成功！";
    }

    @Override
    public List<Student> selectStudentByStudentNumbers(List<String> studentNumbers) {
        return studentMapper.selectStudentByStudentNumbers(studentNumbers);
    }

    @Override
    public List<GraduateStudent> selectGraduateStudentList(GraduateStudent graduateStudent) {
        return studentMapper.selectGraduateStudentList(graduateStudent);
    }

    @Override
    public StudentAuditDetail selectStudentAuditDetail(String studentNumber) {
        return studentMapper.selectStudentAuditDetail(studentNumber);
    }

    private User loadAddStudentToUser(Student student){
        User user = new User();
        BeanUtils.copyProperties(student,user);
        user.setLoginName(student.getStudentNumber());
        user.setPassword("123456");
        user.setRoleId(3L);
        user.setUserName(student.getStudentName());
        user.setSex(StringUtils.equals(student.getGender(),"女") ? "1" : "0");
        user.setUserType("00");
        user.setCreateTime(DateUtils.getNowDate());
        user.setCreateBy(student.getCreateBy());
        return user;
    }

    private User loadUpdateStudentToUser(Student student){
        User user = new User();
        BeanUtils.copyProperties(student,user);
        user.setUserName(student.getStudentName());
        user.setUpdateBy(student.getUpdateBy());
        user.setSex(StringUtils.equals(student.getGender(),"女") ? "1" : "0");
        user.setUpdateTime(DateUtils.getNowDate());
        return user;
    }
}
